package com.yubest.security.filter;

import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.json.JSONUtil;
import com.yubest.security.bean.Response;
import com.yubest.security.consts.Consts;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        Response<Void> data = Response.error(Consts.NOT_PERMIT_CODE, "无操作权限");
        ServletUtil.write(httpServletResponse, JSONUtil.toJsonStr(data), "application/json;charset=utf-8");
    }
}
